//
// Created by ASUS on 2024/11/13/星期三.
//
#include <bits/stdc++.h>
using namespace std;

struct Node{
	Node*son[2]{nullptr};
	int cnt=0;
};

class Trie{
    static const int HIGH_BIT = 31;
public:
	Node*root;
	Trie(){
		root=new Node();
	}

	void insert(int x){
		Node*cur=root;
		for(int i=HIGH_BIT;i>=0;--i){
			int b=x>>i&1;
			if(!cur->son[b]){
				cur->son[b]=new Node();
			}
			cur=cur->son[b];
			cur->cnt++;
		}
	}

	void remove(int x){
		Node*cur=root;
		for (int i = HIGH_BIT; i >= 0; i--) {
			cur = cur->son[(x >> i) & 1];
			cur->cnt--;
		}
	}

	int max_xor(int x,int high=1<<HIGH_BIT) {
		Node* cur = root;
		int ans = 0;
		bool is_limit=true;
		for (int i = HIGH_BIT; i>=0; --i) {
			int b = x >> i & 1;
			if(is_limit){
				if(b==0){
					if(high>>i&1) return -1;
					else if(cur->son[b^1]&&cur->son[b^1]->cnt){
						ans |= 1 << i;
						cur = cur->son[b ^ 1];
					}
				}else{
					if((high>>i&1)&&cur->son[b^1]&&cur->son[b^1]->cnt){
						cur = cur->son[b ^ 1];
						is_limit=false;
					}else if(cur->son[b]&&cur->son[b]->cnt){
						ans |= 1 << i;
						cur = cur->son[b];
					}
				}
			}else{
				if (cur->son[b ^ 1]&&cur->son[b^1]->cnt) {
					ans |= 1 << i;
					cur = cur->son[b ^ 1];
				} else {
					cur = cur->son[b];
				}
			}
		}
		return ans;
	}

	int min_xor(int x) {
		Node *cur = root;
		int ans = 0;
		for (int i = HIGH_BIT; i >= 0; i--) {
			int b = (x >> i) & 1;
			if (cur->son[b] && cur->son[b]->cnt) {
				cur = cur->son[b];
			} else {
				ans |= 1 << i;
				cur = cur->son[b ^ 1];
			}
		}
		return ans;
	}
};